#include <iostream>
#include <cstdint>
#include <mutex>
#include <thread>
#include <chrono>

std::mutex m1;
std::mutex m2;

int gData1 = 100; // 由m1保护
int gData2 = 100; // 由m2保护


void thread_func1()
{
    {
        std::lock_guard<std::mutex> guard1(m1);
        gData1 += 100;
    }


    std::this_thread::sleep_for(std::chrono::seconds(2));

    {
        std::lock_guard<std::mutex> guard2(m2);
        gData2 += 100;
    }

}

void thread_func2()
{
    {
        std::lock_guard<std::mutex> guard2(m2);
        gData2 += 100;
    }

    {
        std::lock_guard<std::mutex> guard1(m1);
        gData1 += 100;
    }

}

int main(int, char**) {
    std::thread t1(thread_func1);
    std::thread t2(thread_func2);

    t1.join();
    t2.join();

    return 0;
}
